<?php
#################################
# Metadata Enhancement for Repositories (MER) 
# This software is an overlay repository search interface that uses a 
# text mined index of terms.
# Copyright (C) 2011  Rory McNicholl (University of London Computer Centre), Robert Drinkall (University College London)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Contact repositories@ulcc.ac.uk
# http://www.ulcc.ac.uk

##################################

include "../mer.php";

$mer = new MER();

$oai_ft_url = $mer->config->repo_url."cgi/oai2?verb=ListRecords&metadataPrefix=oai_dc&set={$mer->config->ep_full_text_set_id}";
print $oai_ft_url."\n";
$oai_ft_str = $mer->do_cURL($oai_ft_url);
$oaixml = new DOMDocument();
$oaixml->loadXML($oai_ft_str);
$oaixpath = new DOMXpath($oaixml);

$result = $oaixpath->query("/*/*/*"); #for some reason any query using the actual name of the element fails...?
$mer->record_count = 0;
foreach ($result as $record) {
	$start = time();
	$mer->record_count++;
	$mer->curr_id = $record->getElementsByTagName("identifier")->item(0)->nodeValue;

	if($mer->record_already()){
		continue;
	}
	$dc = $record->getElementsByTagName("dc")->item(0);
	$digi_objs = $mer->get_digi_objs($dc);

	if(!is_array($digi_objs)){
		switch($digi_objs){
			case -1 : { $mer->message("$oai_url did not resolve"); break;}
			case -2 : { $mer->message("$oai_url did not parse"); break;}
			case -3 : { $mer->message("$oai_url does not appear to be mets formated OAI-PMH record."); break;}
			default : { $mer->message("An unspecified problem with oai_doc (returned *$digi_objs*) $oai_ft_url"); break;}
		}
		continue;
	}
	foreach($digi_objs as $fd){
		//return will an array with either terms_found and terms_inserted or error_num <0 and a message
		$terms = $mer->insert_terms($fd);
		if($terms[0]<0){
			switch($terms[0]){
				case -1 : { $mer->message("[".$fd["bitstream"]."] Unable to turn ".$fd["uri"]." to text: ".$terms[1]); break;}
				case -3 : { $mer->message("[".$fd["bitstream"]."] Termine response could not be parsed: ".$terms[1]); break;}
				default : { $mer->message("[".$fd["bitstream"]."] ".$terms[1]); break;}
			}
			unlink($fd["bitstream"]);
			if(file_exists($fd["bitstream"].".txt")){
				unlink($fd["bitstream"].".txt");
			}
		}else{
			$done = time() - $start;
			$mer->message("SUCCESS!, ".$terms[1]."/".$terms[0]." associations inserted in $done secs");
			$mer->insert_record($id, $terms[1]);
		}
	}

	
}

//finished so remove PID
$mer->remove_PID(getmypid());
#########




